// getSite.js
var bmap = require('../../../common/bmap-wx.min.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sites: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 设置元素高度
    this.getRect();
    // 获取目的城市
    wx.getStorage({
      key: 'EndCity',
      success: function (res) {
        that.setData({
          EndCity: res.data,
        })
      }
    });
  },

  // 存储选择的位置
  getSite: function (e) {
    var obj = new Object();
    obj.site = e.currentTarget.dataset.city + e.currentTarget.dataset.district + e.currentTarget.dataset.name;
    obj.lat = e.currentTarget.dataset.lat;
    obj.lng = e.currentTarget.dataset.lng;
    wx.setStorage({
      key: "getSite",
      data: obj
    })
    wx.navigateBack({});
  },

  getRect: function () {
    var that = this;
    if (wx.createSelectorQuery){
      wx.createSelectorQuery().select('.header').boundingClientRect(function (rect) {
        return that.setData({
          siteListHeight: wx.getSystemInfoSync().windowHeight - rect.height,
        })
      }).exec()
    }
  },

  // 绑定input输入 
  bindKeyInput: function (e) {
    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'D097238894b7c9f3ddf8fd92be560188'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      that.setData({
        sugData: data.result,
        sites: true
      });
    }
    // 发起suggestion检索请求 
    BMap.suggestion({
      query: e.detail.value,
      region: that.data.EndCity,
      city_limit: true,
      fail: fail,
      success: success
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 初始默认地点
    this.initialSite(this.data.EndCity)
  },

  initialSite: function (e) {
    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'D097238894b7c9f3ddf8fd92be560188'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      that.setData({
        sugData: data.result,
        sites: true
      });
    }
    // 发起suggestion检索请求 
    BMap.suggestion({
      query: e,
      region: that.data.EndCity,
      city_limit: true,
      fail: fail,
      success: success
    });
  },

  checkLocation: function (e) {
    var lat = e.currentTarget.dataset.lat,
      lng = e.currentTarget.dataset.lng
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      scale: 14,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  clear: function () {
    wx.navigateBack({});
  }
})